<?php
    require_once("condb.php");
    require_once("functions.php");
    
    $record = 10;
    
    $output = array();
    $info = isset($_GET["info"]) ? $_GET["info"] : "";
    if(!empty($info)){
        $info = urldecode(base64_decode($info));
        parse_str($info, $request);    
        
        $token = isset($request["token"]) ? cleanQuery($request["token"]) : "";
        if($token=="TAoViEC@)!#2012"){
            $data = array();
            
            $data["categoryId"] = isset($request["catId"]) ? intval($request["catId"]) : 0;            
            $data["keyword"] = isset($request["keyword"]) ? cleanQuery($request["keyword"]) : "";            
            $data["p"] = isset($request["p"]) ? intval($request["p"]) : 1;            
            $data["p"] = $data["p"]==0 ? 1 : $data["p"];
            $data["record"] = $record;            

            if(!empty($data["record"])){
                $video = GetAllVideo($data);
                $totalVideo = GetTotalVideo($data);
                $totalPage = ceil($totalVideo / $data["record"]);
                
                foreach($video as $i=>$row){
                    $urlImg = getImage($row["picture"], "video", $row["create_date"], "app");
                    $urlImg_j2me = getImage($row["picture"], "video", $row["create_date"], "m");
                    $item[$i]["id"] = $row["id"]; 
                    $item[$i]["title"] = $row["title"]; 
                    $item[$i]["download"] = ($row["download"] + 200); 
                    $item[$i]["introtext"] = $row["introtext"];                    
                    $item[$i]["file_3gp"] = getImage($row["file"], "video", $row["create_date"], "");                    
                    $item[$i]["file_mp4"] = getImage($row["file_mp4"], "video", $row["create_date"], "");
                    $item[$i]["duration"] = $row["duration"];
                    $item[$i]["src"] = $urlImg;
                    $item[$i]["src_j2me"] = $urlImg_j2me;
                }
                
                $result = array();
                $result["type"] = "ITEM";
                $result["totalPage"] = strval($totalPage);
                $result["action"]["type"] = "aURL";
                $result["action"]["url"] = "/SearchVideo.php?catId=" . $data["categoryId"] . "&p=";
                $result["item"] = isset($item) ? $item : null;
                
                /* Lấy quảng cáo */
                $adv = getAdv();
                if(!empty($adv)){
                    $promotion = array();
                    $promotion["src"] = getImage($adv["picture"], "banner", $adv["create_date"], "");
                    $promotion["link"] = $adv["link"];    
                }
                
                /* Lấy quảng cáo text */
                $advText = getAdvText();
                if(!empty($advText)){
                    $promotionText = array();
                    $promotionText["title"] = $advText["title"];
                    $promotionText["link"] = $advText["link"];    
                }
                
                $output["status"] = 1;
                $output["msg"] = "Success Request"; 
            } else {
                $output["status"] = 4;
                $output["msg"] = "Invalid Request";                        
            }
                
        } else {
            $output["status"] = 3;
            $output["msg"] = "Invalid Request";    
        }
    } else {
        $output["status"] = 2;
        $output["msg"] = "Invalid Request";
    }
            
    $output["data"] = isset($result) ? $result : array();
    $output["promotion"] = isset($promotion) ? $promotion : array();
    $output["promotionText"] = isset($promotionText) ? $promotionText : array();
    
    echo json_encode($output);  
?>